package com.test.tester;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.test.dao.QuestionDao;
import com.test.table.Question;
import com.test.table.TrueAnswer;
import com.test.util.Factory;

public class Exam {

	private class Container{
		
		private Question question;
		private Set<TrueAnswer> answer;
		
		public Container(Question question, Set<TrueAnswer> answer){
			this.question = question;
			this.answer = answer;
		}
		
		public boolean isTrue(){			
			if(question.getTrueAnswer().equals(answer)){
				return true;
			}
			return false;
		}	
	}
	
	private Map<Integer,Container> list = new HashMap<>();
	private List<Question> questions = new ArrayList<>(); 

	public Exam(){
		Factory factory = Factory.getInstance();
		QuestionDao questionDao = factory.getQuestionDao();
		try {
			List<Question> question = questionDao.getAllQuestions();
			Collections.shuffle(question);
			questions = question.subList(0, 9);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public List<Question> getQuestions(){
		return questions;
	}
		
	public void addAnswers(Map<String,Set<TrueAnswer>> answers){
		for(int i = 0 ; i < questions.size(); i++){
			if(answers.get(questions.get(i).getQuestion())!= null){
				list.put(i,new Container(questions.get(i),answers.get(questions.get(i).getQuestion())));
			}else{
				list.put(i,new Container(questions.get(i),new HashSet<TrueAnswer>()));
			}			
		}
	}
		
	public int getResult(){
		int result = 0;
		for(Integer index = 0 ; index < list.size() ; index++){
			if(list.get(index).isTrue()){
				result++;
			}
		}
		return result;
	}
	
}
